<?php

namespace App\Http\Requests;

use App\Constant\Error;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;

class ApplyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        //echo getOpenid();exit;
//        if (empty(getOpenid())) {
//            return false;
//        }
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $provinces = implode(',', array_keys(config('admin.location.province')));
        $rules['name'] = 'required|max:50';
        $rules['sex'] = 'required|numeric|in:1,2';
        $rules['province'] = "required|in:$provinces";
        $rules['city'] = 'required';
        $rules['school'] = 'required';//|exists:schools,name
        $rules['education'] = 'required|numeric|min:1';
        $rules['grade'] = 'required|numeric|min:1';
        $rules['degree'] = 'required|numeric|between:1,4';
        $rules['phone'] = ['required', 'regex:/1[3|4|5|7|8][0-9]\d{4,8}/'];
        $rules['wechat'] = 'required';
        $rules['qq'] = 'required';
        $rules['email'] = 'required|email';
        $rules['photo'] = 'required|url';
        $rules['apply'] = 'required|numeric|between:1,14';
        $rules['applyDetail'] = 'required|max:2048';//json
        $rules['organization'] = 'required|max:510';//json
        $rules['innerActivity'] = 'required|max:510';
        $rules['meizuOrg'] = 'required|max:510';//json
        $rules['outerOrg'] = 'required|max:510';//json
        $rules['outerActivity'] = 'required|max:510';
        if (request()->isMethod('PUT') || request()->isMethod('PATCH')) {
            // 修改时 request()->method() 方法返回的是 PUT或PATCH
            $rules['id'] = 'numeric|required';
        }
        return $rules;
    }

    /**
     * 验证信息
     * @author 晚黎
     * @date   2016-11-02T10:25:59+0800
     * @return [type]                   [description]
     */
    public function messages()
    {
        return [
            'required' => trans('validation.required'),
            'numeric' => trans('validation.numeric'),
            'in' => trans('validation.in'),
        ];
    }

    /**
     * 字段名称
     * @author 晚黎
     * @date   2016-11-02T10:28:52+0800
     * @return [type]                   [description]
     */
    public function attributes()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'sex' => '性别',
            'province' => '省份',
            'city' => '城市',
            'school' => '学校',
            'zone' => '校区',
            'faculty' => '院系',
            'profession' => '专业',
            'education' => '学制',
            'grade' => '年级',
            'phone' => '手机号',
            'wechat' => '微信',
            'qq' => 'QQ',
            'email' => '邮箱',
            'photo' => '照片',
            'apply' => '岗位类型',
            'applyDetail' => '岗位详情',
            'organization' => '校内组织',
            'innerActivity' => '校内活动',
            'meizuOrg' => '魅族组织',
            'outerOrg' => '校外组织',
            'outerActivity' => '校外活动',
        ];
    }

    public function response(array $errors)
    {
        $error = Error::getError(Error::FORM_VALIDATION_ERROR, null, $errors);
        return new JsonResponse($error, 422);
    }
}
